package com.ruijie.acl.web.config;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.web.action.BasePagedSupportAction;

/**
 * <p>Title:  ACL组选择器件中负责已选ACL组的类    </p>
 * <p>Description: Function Description </p>
 * <p>Copyright: Copyright (c) 2009     </p>
 * <p>Company: Ruijie Co., Ltd.       </p>
 * <p>Create Time: 2009-09-22           </p>
 * @author ASAI
 * <p>Update Time:                      </p>
 * <p>Updater:                          </p>
 * <p>Update Comments:                  </p>
 */
public class AccessGroupSelectedSelector extends BasePagedSupportAction {

    private AccessGroupService selectorService;

    private AccessGroup accessGroup = new AccessGroup();

    private boolean reQueryAllSelected = false;

    private AccessGroupAvailableSelector availableSelector;

    private List<AccessGroup> allSelectedDevices;
    
    private List<Long> selectedAccessGroupList = new ArrayList<Long>();

	public AccessGroupSelectedSelector(AccessGroupService selectorService) {
        this.selectorService = selectorService;
    }

    @Override
    protected void updateModel() {
        currentPage = selectorService.pagedQuerySelectedAccessGroups(selectedAccessGroupList.toArray(), page, pageSize);

        // 如果当前页面已经没有记录(例如删除了当前页面的所有记录)，那么显示上一页
        if (currentPage.getTotalPageCount() < page && page > 1) {
            page--;
            updateModel();
        }

        //selectedIds.clear();
    }

    @SuppressWarnings("unchecked")
    ListDataModel getSelectedDataModel() {
       // if (null == currentPage)
            updateModel();

        return new ListDataModel((List<AccessGroup>) currentPage.getResult());
    }

    /**	
     * <p>Description: 不分页场景 </p>
     * <p>Create Time: Dec 9, 2008   </p>
     * @author ASAI
     * @return
     */
    @SuppressWarnings("unchecked")
    ListDataModel getSelectedDataModelNoPage() {
        if (null == allSelectedDevices || reQueryAllSelected){
            //updateAllSelectedDevices();
        	Page page = selectorService.pagedQuerySelectedAccessGroups(
        			selectedAccessGroupList.toArray(), 1, 10000);
        	allSelectedDevices = (List<AccessGroup>) page
					.getResult();
        }
        reQueryAllSelected = false;
        
        return new ListDataModel(allSelectedDevices);
    }

    //private void updateAllSelectedDevices() {
        //allSelectedDevices = selectorService.queryAllSelectedDevices(uuid);
    //}

    /**	
     * <p>Description: 获取所有已经选择的ACL组 </p>
     * <p>Create Time: Dec 10, 2008   </p>
     * @author ASAI
     * @return
     */
    List<AccessGroup> getAllSelectedDevices() {
        getSelectedDataModelNoPage();

        return allSelectedDevices;
    }

    public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	/**	
     * <p>Description: 删除已选择ACL组,ACL组由页面上的复选框指定 </p>
     * <p>Create Time: 2009-09-22   </p>
     * @author ASAI
     */
    public void unselect() {
    	Long[] ids = this.getIds();
     	for(Long id : ids){
     		this.getSelectedAccessGroupList().remove(id);
     	}
        //selectorService.unselect(uuid, getIds());
        notifyChange();
    }
    
    /**	
     * <p>Description:  删除指定ACL组  </p>
     * <p>Create Time: 2009-09-22   </p>
     * @author ASAI
     * @param id
     */
    public void unselect(Long id) {
        //selectorService.unselect(uuid, new Long[]{id});
    	this.getSelectedAccessGroupList().remove(id);
        notifyChange();
    }
    
    /**	
     * <p>Description: 删除指定ACL组   </p>
     * <p>Create Time: 2009-09-22   </p>
     * @author ASAI
     * @param ids
     */
    public void unselect(Long[] ids) {
    	for(Long id : ids){
     		this.getSelectedAccessGroupList().remove(id);
     	}
        notifyChange();
    }

    /**	
     * <p>Description: 删除所有已经选择的ACL组</p>
     * <p>Create Time: 2009-09-22   </p>
     * @author ASAI
     */
    public void unselectAll() {
    	this.getSelectedAccessGroupList().clear();
        notifyChange();
    }

    public void setAvailableSelector(AccessGroupAvailableSelector availableSelector) {
        this.availableSelector = availableSelector;
    }

    /**	
     * <p>Description: 通知相关的数据模型更新</p>
     * <p>Create Time: 2009-09-22   </p>
     * @author ASAI
     */
    private void notifyChange() {
        refresh();
        availableSelector.refresh();
    }
    
   @Override
    public void refresh() {
        super.refresh();
        reQueryAllSelected = true;
        this.selectedIds.clear();
    }
   
   public List<Long> getSelectedAccessGroupList() {
		return selectedAccessGroupList;
	}

	public void setSelectedAccessGroupList(List<Long> selectedAccessGroupList) {
		this.selectedAccessGroupList = selectedAccessGroupList;
	}
}
